package com.taotao.sys.controller;


import com.taotao.common.BusinessException;
import com.taotao.common.Pagination;
import com.taotao.common.RespData;
import com.taotao.common.auth.AuthRequired;
import com.taotao.common.auth.CurrUserId;
import com.taotao.common.auth.UserInfo;
import com.taotao.sys.model.vo.user.UpdatePwdPram;
import com.taotao.sys.model.vo.user.UserAccountPageParam;
import com.taotao.sys.model.vo.user.UserAccountPageVO;
import com.taotao.sys.model.vo.user.UserAccountParam;
import com.taotao.sys.service.SysUserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;
import java.util.Set;

/**
 *
 * @author youbeiwuhuan
 */
@Api(tags = "用户模块")
@Validated
@RestController
@RequestMapping("/user")
public class SysUserAccountController {

    @Autowired
    private SysUserAccountService sysUserAccountService;


    @AuthRequired
    @ApiOperation(value = "分页列表", notes = "")
    @PostMapping(value = "/pageList")
    public RespData<Pagination<UserAccountPageVO>> pageList(@RequestBody UserAccountPageParam param) {

        return RespData.successResp(sysUserAccountService.pageList(param));
    }

    @AuthRequired
    @ApiOperation(value = "重置密码", notes = "")
    @PostMapping(value = "/updatePwd")
    public RespData<UserInfo> updatePwd(@Valid @RequestBody UpdatePwdPram updatePwdPram, @CurrUserId Long userId) {

        if (!Objects.equals(updatePwdPram.getPassword(), updatePwdPram.getRePassword())) {
            throw new BusinessException("两次输入密码不一致");
        }

        sysUserAccountService.updatePwd(userId, updatePwdPram.getPassword());

        return RespData.successResp();
    }

    @AuthRequired
    @ApiOperation(value = "新增或编辑", notes = "")
    @PostMapping(value = "/addOrUpdate")
    public RespData<UserInfo> addOrUpdate(@Valid @RequestBody UserAccountParam param, @CurrUserId Long userId) {

        sysUserAccountService.addOrUpdate(param, userId);

        return RespData.successResp();
    }

    @AuthRequired
    @ApiOperation(value = "批量删除", notes = "")
    @PostMapping(value = "/deleteByIds")
    public RespData<UserInfo> deleteByIds(@Valid @RequestBody Set<Long> ids, @CurrUserId Long userId) {
        sysUserAccountService.deleteByIds(ids, userId);
        return RespData.successResp();
    }

    @AuthRequired
    @ApiOperation(value = "批量禁用/启用", notes = "")
    @PostMapping(value = "/changeStatus")
    public RespData<UserInfo> changeStatus(@Valid @RequestBody Set<Long> ids, @RequestParam("status") Integer status, @CurrUserId Long userId) {
        sysUserAccountService.changeStatus(ids,status, userId);
        return RespData.successResp();
    }


}
